<html>
<head><meta charset="utf-8"><title>Example of outlives constraint · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html">Example of outlives constraint</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="204726816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204726816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nell Shamrell-Harrington <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204726816">(Jul 22 2020 at 21:49)</a>:</h4>
<p>Hello all -</p>
<p>I'm continuing my journey deep into the internals of the Rust compiler.  Right now I'm working on region inference and constraint propogation.  It's pretty straightforward to come up with a practical example that violates the liveness constraint, but I'm having a hard time coming up with a real world example of the outlives constraint. I understand what it's doing based on the pseudocode included here <a href="https://rustc-dev-guide.rust-lang.org/borrow_check/region_inference/constraint_propagation.html">https://rustc-dev-guide.rust-lang.org/borrow_check/region_inference/constraint_propagation.html</a> here, but I'm having a hard time coming up with an example I can use when presenting.</p>
<p>Does anyone have or know of an example of code that illustrates the outlives constraint?</p>



<a name="204759157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759157">(Jul 23 2020 at 04:14)</a>:</h4>
<p><span class="user-mention" data-user-id="225192">@Nell Shamrell-Harrington</span> part of the problem is that if you write <code>&amp;'long &amp;'short T</code>, most positions you can write that in will require that <code>'long</code> is shorter than (or equal to) <code>'short</code> automatically, so if you already have the opposite constraint, you end up with effectively a constraint that they're equal instead</p>



<a name="204759162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759162">(Jul 23 2020 at 04:14)</a>:</h4>
<p>you probably need associated types in weird positions, to observe this</p>



<a name="204759202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759202">(Jul 23 2020 at 04:15)</a>:</h4>
<p><span class="user-mention" data-user-id="225192">@Nell Shamrell-Harrington</span> ah no I was misremembering, you just need to write the type in a <code>where</code> clause or equivalent e.g. <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a3a5adf915d5345de490d4f3e04f9d33">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a3a5adf915d5345de490d4f3e04f9d33</a></p>



<a name="204759247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759247">(Jul 23 2020 at 04:16)</a>:</h4>
<p>we theoretically could <em>also</em> infer the constraint from that, idk why we don't, it might be arbitrary</p>



<a name="204759261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759261">(Jul 23 2020 at 04:16)</a>:</h4>
<p>I don't know how you would get a borrowck error from this though</p>



<a name="204759337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759337">(Jul 23 2020 at 04:18)</a>:</h4>
<p><span class="user-mention" data-user-id="225192">@Nell Shamrell-Harrington</span> maybe this? <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=c1048e7e7a73a684a721a3b2373edd6d">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=c1048e7e7a73a684a721a3b2373edd6d</a></p>



<a name="204759448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759448">(Jul 23 2020 at 04:20)</a>:</h4>
<p>lol you get wild results if you try to get an outlives error <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=e7db58988a9e3393d4a6b2e6366e108d">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=e7db58988a9e3393d4a6b2e6366e108d</a></p>



<a name="204759467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759467">(Jul 23 2020 at 04:21)</a>:</h4>
<p><span class="user-mention" data-user-id="225192">@Nell Shamrell-Harrington</span> so I guess I'd say "outlives" constraints are not generally observed as such because they feed into inference and you generally get a different error <em>elsewhere</em>. but <span class="user-mention" data-user-id="116009">@nikomatsakis</span> would know more</p>



<a name="204759515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759515">(Jul 23 2020 at 04:22)</a>:</h4>
<p>like in this case it thinks <code>y</code> may borrow <code>x</code> due to the signature of <code>foo</code></p>



<a name="204759574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759574">(Jul 23 2020 at 04:24)</a>:</h4>
<p><code>&amp;x</code> creates a <code>&amp;'b i32</code> that has to outlive <code>'a</code> which means <code>'b</code> has to outlive <code>'a</code> (which would be <code>'static</code> if I could actually feed that requirement in)</p>



<a name="204759601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759601">(Jul 23 2020 at 04:25)</a>:</h4>
<p>I can do this but it doesn't really do anything useful <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a649676eacea5e6b26e619e7e5ffa1da">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=a649676eacea5e6b26e619e7e5ffa1da</a></p>



<a name="204759610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204759610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204759610">(Jul 23 2020 at 04:25)</a>:</h4>
<p>or different, I should say</p>



<a name="204824439"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204824439" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nell Shamrell-Harrington <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204824439">(Jul 23 2020 at 17:00)</a>:</h4>
<p>Thank you!</p>



<a name="204825492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Example%20of%20outlives%20constraint/near/204825492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint.html#204825492">(Jul 23 2020 at 17:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="119009">eddyb</span> <a href="#narrow/stream/131828-t-compiler/topic/Example.20of.20outlives.20constraint/near/204759261">said</a>:</p>
<blockquote>
<p>I don't know how you would get a borrowck error from this though</p>
</blockquote>
<p>you never would <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span> <a href="https://github.com/rust-lang/rust/issues/25860">https://github.com/rust-lang/rust/issues/25860</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>